import json
import unittest
from api.login import LoginApi
from parameterized import parameterized
import app


def build_data():
    test_data = []
    json_file = app.BASE_DIR + "/data/login.json"
    with open(json_file, encoding="utf-8") as f:
        json_data = json.load(f)
        # print(json_data)
        for case_data in json_data:
            login_data = case_data.get("login_data")
            status_code = case_data["status_code"]
            success = case_data["success"]
            code = case_data["code"]
            message = case_data["message"]
            test_data.append((login_data, status_code, success, code, message))
    return test_data


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginApi()

    @parameterized.expand(build_data())
    def test01_login(self, login_data, status_code, success, code, message):

        response = self.login_api.login(login_data)
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(success, response.json().get("success"))
        self.assertEqual(code, response.json().get("code"))
        self.assertIn(message, response.json().get("message"))

        # print(response.json())

        app.TOKEN = "Bearer " + response.json().get("data")
        print(app.TOKEN)
        app.headers_data["Authorization"] = app.TOKEN
        print(app.headers_data["Authorization"])
